package cau.se.project.data;

import java.util.ArrayList;
import java.util.List;

import cau.se.project.data.method.RowControl;
import cau.se.project.method.DSMControl;

public class DependencyStructureMatrix implements DSMControl {

	List<RowControl> m_rowList = new ArrayList<>();
	@Override
	public RowControl makeRow(String name) {
		return new DSMRow(name);
	}

	@Override
	public void removeRow(RowControl row) {
		m_rowList.remove(row);
	}

	@Override
	public void insertRow(RowControl rowInsert, RowControl rowPosition) {
		if(rowPosition == null) {
			m_rowList.add(rowInsert);
		}
		else {
			int index = m_rowList.indexOf(rowPosition);
			m_rowList.add(index, rowInsert);
		}
	}

	@Override
	public int[][] toArray() {
		int[][] arrayRet = new int[m_rowList.size()][m_rowList.size()];
		
		int i=0;
		for(RowControl rc : m_rowList) {
			//arrayRet[i] = new int[m_rowList.size()];
			for(int j=0; j<m_rowList.size(); j++) {
				if(rc.getDepenencyList().contains(m_rowList.get(j)) == true) {
					arrayRet[i][j] = 1;
				}
				else {
					arrayRet[i][j] = 0;
				}
			}
			i++;
		}
		
		return arrayRet;
	}

	@Override
	public int getSize() {
		return m_rowList.size();
	}

	@Override
	public RowControl getRow(int i) {
		RowControl row = null;
		try {
			row = m_rowList.get(i);
		}
		catch(IndexOutOfBoundsException e) {
			row = null;
		}

		return row;
	}

	@Override
	public RowControl findRowByName(String name) {
		for(RowControl rc : m_rowList) {
			if(rc.getName().equals(name)) {
				return rc;
			}
		}
		
		return null;
	}
	
	@Override
	public void print()
	{
		int[][] arr = toArray();
		System.out.println("print array");
		for(int i=0; i<arr.length; i++) {
			for(int j=0; j<arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
	}
}
